using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using Commerce.Providers;
using SubSonic;

namespace CommerceForUmbraco.usercontrols
{
    public partial class BillAddressEntry : System.Web.UI.UserControl
    {
        bool countrySet = false;
        Address address = null;
        private string _Email;
      
        public Address SelectedAddress
        {
            get
            {

                //return the entered text bits as an address
                address = new Address();
                address.FirstName = txtFirst.Text;
                address.LastName = txtLast.Text;
                address.Address1 = txtAddress1.Text;
                address.City = txtCity.Text;
                address.Email = txtEmail.Text;
                address.Address2 = txtAddress2.Text;
                address.Zip = txtZip.Text;
                //address.Phone = txtPhone.Text;

                address.StateOrRegion = stateCode.SelectedValue;
                //address.StateOrRegion = stateCode.Text;
                address.Country = ddlCountry.SelectedValue;

                return address;

            }
            set
            {
                address = value;
                txtFirst.Text = address.FirstName;
                txtLast.Text = address.LastName;
                txtAddress1.Text = address.Address1;
                txtCity.Text = address.City;
                txtEmail.Text = address.Email;
                txtAddress2.Text = address.Address2;
                txtZip.Text = address.Zip;
                //txtPhone.Text = address.Phone;
                stateCode.Text = address.StateOrRegion;

                if (address.Country != null)
                {
                    if (address.Country.Trim().Length == 2)
                    {
                        ddlCountry.SelectedValue = address.Country;
                        countrySet = true;
                    }
                }

            }
        }

        public string Email
        {
            get { return _Email; }
            set 
            { 
                _Email = value;
                txtEmail.Text = _Email;
            }
        }
	
        
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!Page.IsPostBack)
            {
                if (!countrySet)
                {
                    ddlCountry.SelectedValue = "US";
                }
            }
            else
            {
                Page.Validate();
            }
        }

        protected void ServerValidation(object source, ServerValidateEventArgs args)
        {
            // only allow 2-character state codes
            if (stateCode.Text.Length == 2)
                args.IsValid = true;
            else
                args.IsValid = false;
            
        }

        

    }
}
